
import 'package:flutter/material.dart';
import 'package:fluttertenroadui/common/defines.dart';

class TLFlatButton extends StatelessWidget {

  const TLFlatButton({
    Key key,
    this.onPressed,
    this.onHighlightChanged,
    this.textTheme,
    this.textColor,
    this.disabledColor,
    this.focusColor,
    this.hoverColor,
    this.color,
    this.disabledTextColor,
    this.highlightColor,
    this.splashColor,
    this.colorBrightness,
    this.padding,
    this.shape,
    this.clipBehavior,
    this.materialTapTargetSize,
    this.focusNode,
    this.expanded = false,
    @required this.child,
  }) : super(key : key);

  final VoidCallback onPressed;
  final ValueChanged<bool>  onHighlightChanged;
  final ButtonTextTheme textTheme;
  final Color textColor;
  final Color disabledTextColor;
  final Color color;
  final Color disabledColor;
  final Color focusColor;
  final Color hoverColor;
  final Color highlightColor;
  final Color splashColor;
  final Brightness colorBrightness;
  final EdgeInsetsGeometry padding;
  final ShapeBorder shape;
  final Clip  clipBehavior;
  final FocusNode focusNode;
  final MaterialTapTargetSize materialTapTargetSize;
  final bool expanded;
  final Widget child;

  factory TLFlatButton.icon({
    Key key,
    @required VoidCallback onPressed,
    ValueChanged<bool> onHighlightChanged,
    ButtonTextTheme textTheme,
    Color textColor,
    Color disabledTextColor,
    Color color,
    Color disabledColor,
    Color focusColor,
    Color hoverColor,
    Color highlightColor,
    Color splashColor,
    Brightness colorBrightness,
    EdgeInsetsGeometry padding,
    ShapeBorder shape,
    Clip clipBehavior,
    FocusNode focusNode,
    MaterialTapTargetSize materialTapTargetSize,
    TLPosition iconPosition,
    double spacing,
    @required Widget icon,
    @required Widget label,
  }) = _TLFlatButtonWithIcon;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    final Widget buttonChild = expanded
        ? Flex(
            direction: Axis.horizontal,
            children: <Widget>[
              Expanded(
                child: child,
              ),
            ],
          )
        : child;

      return FlatButton(
          onPressed: onPressed,
          onHighlightChanged: onHighlightChanged,
          textTheme: textTheme,
          textColor: textColor,
          disabledTextColor: disabledTextColor,
          color: color,
          disabledColor: disabledColor,
          hoverColor: hoverColor,
          focusColor: focusColor,
          highlightColor: highlightColor,
          splashColor: splashColor,
          colorBrightness: colorBrightness,
          padding: padding,
          shape: shape,
          clipBehavior: clipBehavior ?? Clip.none,
          focusNode: focusNode,
          materialTapTargetSize: materialTapTargetSize,
          child: buttonChild);
  }

}

class _TLFlatButtonWithIcon extends TLFlatButton
  with MaterialButtonWithIconMixin {

  _TLFlatButtonWithIcon({
    Key key,
    @required VoidCallback onPressed,
    ValueChanged<bool> onHighlightChanged,
    ButtonTextTheme textTheme,
    Color textColor,
    Color disabledTextColor,
    Color color,
    Color disabledColor,
    Color hoverColor,
    Color focusColor,
    Color highlightColor,
    Color splashColor,
    Brightness colorBrightness,
    EdgeInsetsGeometry padding,
    ShapeBorder shape,
    Clip clipBehavior,
    FocusNode focusNode,
    MaterialTapTargetSize materialTapTargetSize,
    this.iconPosition,
    this.spacing,
    @required this.icon,
    @required this.label,
  }) : super (
      key: key,
      onPressed: onPressed,
      onHighlightChanged: onHighlightChanged,
      textTheme: textTheme,
      textColor: textColor,
      disabledTextColor: disabledTextColor,
      color: color,
      disabledColor: disabledColor,
      hoverColor: hoverColor,
      focusColor: focusColor,
      highlightColor: highlightColor,
      splashColor: splashColor,
      colorBrightness: colorBrightness,
      padding: padding,
      shape: shape,
      clipBehavior: clipBehavior,
      focusNode: focusNode,
      materialTapTargetSize: materialTapTargetSize,
      child: null);

  final TLPosition iconPosition;
  final double spacing;
  final Widget icon;
  final Widget label;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    final bool isHorizontal = (iconPosition == TLPosition.left || iconPosition == TLPosition.right);
    final SizedBox spaceBox = isHorizontal ? SizedBox(width: spacing,) : SizedBox(height: spacing,);
    final List<Widget> children = (iconPosition == TLPosition.left || iconPosition == TLPosition.top) ?
        [icon, spaceBox, label]
        :[label, spaceBox, icon];
    final Widget buttonChild = isHorizontal ? Row(mainAxisSize: MainAxisSize.min, children: children,) : Column(mainAxisSize: MainAxisSize.min, children: children,);
    return FlatButton(
        onPressed: onPressed,
        onHighlightChanged: onHighlightChanged,
        textTheme: textTheme,
        textColor: textColor,
        disabledTextColor: disabledTextColor,
        color: color,
        disabledColor: disabledColor,
        focusColor: focusColor,
        hoverColor: hoverColor,
        highlightColor: highlightColor,
        splashColor: splashColor,
        colorBrightness: colorBrightness,
        padding: padding,
        shape: shape,
        clipBehavior: clipBehavior ?? Clip.none,
        focusNode: focusNode,
        materialTapTargetSize: materialTapTargetSize,
        child: buttonChild);
  }
}